Skip to content

Conversation

ostrolucky
Copy link
Member

Fixes #1884

$id = $reference->__toString();
foreach ($container->getDefinition($id)->getTag('doctrine.orm.entity_listener') as $attributes) {
foreach ($container->findTaggedServiceIds('doctrine.orm.entity_listener') as $id => $tags) {
usort($tags, static fn (array $a, array $b) => ($a['priority'] ?? 0) <=> ($b['priority'] ?? 0));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be

Suggested change
usort($tags, static fn (array $a, array $b) => ($a['priority'] ?? 0) <=> ($b['priority'] ?? 0));
usort($tags, static fn (array $a, array $b) => ($b['priority'] ?? 0) <=> ($a['priority'] ?? 0));

i.e. tags with a larger number as priority come first!?

see PriorityTaggedServiceTrait#L113

foreach ($resolvers as $reference) {
$id = $reference->__toString();
foreach ($container->getDefinition($id)->getTag('doctrine.orm.entity_listener') as $attributes) {
foreach ($container->findTaggedServiceIds('doctrine.orm.entity_listener') as $id => $tags) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next line will sort the tags within the service by priority but the services are now simply in the order they are returned by $container->findTaggedServiceIds(), i.e., in the order they were defined.
But for the listeners to be registered in the correct order the tags of all services need to be ordered together.

This probably needs to restructure the code to a single foreach over all tags of all returned services?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So something like this:

        $serviceTags = [];
        foreach ($container->findTaggedServiceIds('doctrine.orm.entity_listener') as $id => $tags) {
            foreach ($tags as $attributes) {
                $serviceTags[] = [
                    'serviceId' => $id,
                    'attributes' => $attributes,
                ];
            }
        }

        usort($serviceTags, static fn (array $a, array $b) => ($b['attributes']['priority'] ?? 0) <=> ($a['attributes']['priority'] ?? 0));

        foreach ($serviceTags as $tag) {
            $id            = $tag['serviceId'];
            $attributes    = $tag['attributes'];

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test I have added doesn't pass with such a change. Mind raising a PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2029

@greg0ire greg0ire changed the base branch from 2.14.x to 2.16.x September 4, 2025 11:58
@ostrolucky ostrolucky closed this Sep 10, 2025
@ostrolucky ostrolucky deleted the fix-1884 branch September 10, 2025 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[EntityListener] with multiple listeners per class only the first priority is used
2 participants